﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions;

namespace Topro.Application.Impl
{
    using Topro.Application.Interface;
    using Topro.Db;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.Inventory;
    using Topro.Entity.Response.Inventory;
    using Topro.Entity.Tables;
    using Topro.Log;
    using Topro.Util;

    /// <summary>
    /// 库存业务接口实现
    /// </summary>
    public class InventoryServiceImpl: InventoryService
    {
        #region 注入

        /// <summary>
        /// 
        /// </summary>
        private readonly IUId _uid;

        /// <summary>
        /// db
        /// </summary>
        private readonly IDb _db;

        /// <summary>
        /// loggerService
        /// </summary>
        private readonly LoggerService _loggerService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dapperFactory"></param>
        /// <param name="logger"></param>
        public InventoryServiceImpl(IDb db, LoggerService loggerService, IUId uid)
        {
            _uid = uid;
            _db = db;
            _loggerService = loggerService;
        }
        #endregion

        /// <summary>
        /// 获取物料
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public InventoryResponse Get(int? id)
        {
            var inventory = new InventoryResponse();

            if (id.HasValue)
            {
                inventory = _db.DbConnection.QueryFirst<InventoryResponse>("select * from t_inventory where Id=@Id", new { Id = id });
            }

            return inventory;
        }

        /// <summary>
        /// 保存、修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JResult Save(SaveInventoryRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            if (request.Id > 0)
            {
                if (request.Qty <= 0)
                {
                    throw new Exception("数量必须为正整数");
                }
                var inventory = _db.DbConnection.QueryFirst<Inventory>("select * from t_inventory where Id=@Id", new { Id = request.Id });
                if (inventory == null)
                {
                    throw new Exception("权限记录不存在.");
                }

                var message = string.Format("源数据：料号({0})、物料名称({1})、数量({2})、批号({3})，", inventory.PartNo, inventory.ProductName, inventory.Qty, inventory.LotNo);

                inventory.PartNo = request.PartNo;
                inventory.ProductName = request.ProductName;
                inventory.Qty = request.Qty.Value;
                inventory.LotNo = request.LotNo;
                inventory.Remark = request.Remark;

                message += string.Format("新数据：料号({0})、物料名称({1})、数量({2})、批号({3})", inventory.PartNo, inventory.ProductName, inventory.Qty, inventory.LotNo);

                _loggerService.Add(new OperLog()
                {
                    Level = 1,
                    Source = 1,
                    Message = message,
                    Ip = request.Ip,
                    CreatedBy = request.UserId
                });
                //更新
                _db.DbConnection.Update<Inventory>(inventory);
                response.Success = true;
            }
            else
            {

            }
            return response;
        }

        /// <summary>
        /// 入库查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataGrid<InventoryQueryResponse> StockInQuery(InventoryQuery query)
        {
            var files = "ti.Id,ti.OrderType,ti.ResourceId,ti.WarehouseUnitId,wu.Code as WarehouseUnitTostring,ti.ContainerCode,ti.IsContainer,ti.Coordinate,ti.PartNo,ti.ProductName,ti.Grade,ti.LotNo,ti.Status,ti.Qty,ti.Remark,u.Account as CreatedByTostring,ti.CreateTime";
            var tableName = "t_inventory ti LEFT JOIN t_warehouseunit wu ON ti.WarehouseUnitId=wu.Id LEFT JOIN user u ON ti.CreatedBy=u.Id";
            var orderby = "ORDER BY ti.Id ASC";
            var where = "where 1=1";

            #region 条件
            if (query.OrderType.HasValue)
                where += string.Format(" And ti.OrderType ={0}", query.OrderType.Value);
            //状态
            if (query.Status.HasValue)
                where += string.Format(" And ti.Status ={0}", query.Status.Value);
            //库存数量
            if (query.InventoryQty.HasValue)
                where += string.Format(" And ti.InventoryQty >{0}", query.InventoryQty.Value);
            //料号
            if (!string.IsNullOrEmpty(query.PartNo))
                where += string.Format(" And ti.PartNo like '%{0}%'", query.PartNo);
            //料号
            if (!string.IsNullOrEmpty(query.PartNo))
                where += string.Format(" And ti.PartNo like '%{0}%'", query.PartNo);
            //批号
            if (!string.IsNullOrEmpty(query.LotNo))
                where += string.Format(" And ti.LotNo like '%{0}%'", query.LotNo);
            //排
            if (query.Pai.HasValue)
                where += string.Format(" And wu.Pai ={0}", query.Pai.Value);
            //列
            if (query.Lie.HasValue)
                where += string.Format(" And wu.Lie ={0}", query.Lie.Value);
            //层
            if (query.Ceng.HasValue)
                where += string.Format(" And wu.Ceng ={0}", query.Ceng.Value);
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("SELECT COUNT(1) FROM {0} {1};", tableName, where);
            //sb.AppendFormat("select {0} from {1} {2} {3} offset {4} rows fetch next {5} rows only", files, tableName, where, orderby, pageIndex, query.PageSize);
            sb.AppendFormat("SELECT {0} FROM {1} {2} {3} LIMIT {4},{5};", files, tableName, where, orderby, query.Start, query.Rows);
            using (var multi = _db.DbConnection.QueryMultiple(sb.ToString()))
            {
                var count = multi.ReadFirst<int>();

                var list = multi.Read<InventoryQueryResponse>();

                var rows = new List<InventoryQueryResponse>();

                foreach(var item in list)
                {
                    var row =rows.FirstOrDefault(c => c.ResourceId == item.ResourceId);
                    if(row != null)
                    {
                        //空
                        item.ResourceId = String.Empty;
                        rows.Add(item);
                    }
                    else
                        rows.Add(item);
                }

                return new DataGrid<InventoryQueryResponse>()
                {
                    Rows = rows,
                    Total = count
                };
            }
        }

        /// <summary>
        /// 库存查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataGrid<InventoryQueryResponse> StockOutQuery(InventoryQuery query)
        {
            var pageIndex = query.PageIndex <= 0 ? 0 : (query.PageIndex - 1) * query.PageSize;

            var files = "ti.Id,ti.OrderType,ti.ResourceId,ti.SourceId,ti.WarehouseUnitId,wu.Code as WarehouseUnitTostring,ti.ContainerCode,ti.IsContainer,ti.Coordinate,ti.PartNo,ti.ProductName,ti.Grade,ti.LotNo,ti.Status,ti.Qty,ti.Remark,u.Account as CreatedByTostring,ti.CreateTime";
            var tableName = "t_inventory ti LEFT JOIN t_warehouseunit wu ON ti.WarehouseUnitId=wu.Id LEFT JOIN user u ON ti.CreatedBy=u.Id";
            var orderby = "ORDER BY ti.Id ASC";
            var where = "where 1=1";

            #region 条件
            if (query.OrderType.HasValue)
            {
                where += string.Format(" And ti.OrderType ={0}", query.OrderType.Value);
            }
            //状态
            if (query.Status.HasValue)
            {
                where += string.Format(" And ti.Status ={0}", query.Status.Value);
            }
            ////容器类型
            //if (query.ContainerType.HasValue)
            //{
            //    where += string.Format(" And ti.ContainerType ={0}", query.ContainerType.Value);
            //}
            ////是否空托盘
            //if (query.IsContainer.HasValue)
            //{
            //    where += string.Format(" And ti.IsContainer ={0}", query.IsContainer.Value);
            //}
            ////库存
            //if (query.StockQuantity.HasValue)
            //{
            //    where += string.Format(" And ti.StockQuantity >{0}", query.StockQuantity.Value);
            //}
            ////订单号
            //if (!string.IsNullOrEmpty(query.OrderId))
            //{
            //    where += string.Format(" And ti.OrderId like '%{0}%'", query.OrderId);
            //}
            //料号
            if (!string.IsNullOrEmpty(query.PartNo))
            {
                where += string.Format(" And ti.PartNo like '%{0}%'", query.PartNo);
            }
            //批号
            if (!string.IsNullOrEmpty(query.LotNo))
            {
                where += string.Format(" And ti.LotNo like '%{0}%'", query.LotNo);
            }
            //排
            if (query.Pai.HasValue)
            {
                where += string.Format(" And wu.Pai ={0}", query.Pai.Value);
            }
            //列
            if (query.Lie.HasValue)
            {
                where += string.Format(" And wu.Lie ={0}", query.Lie.Value);
            }
            //层
            if (query.Ceng.HasValue)
            {
                where += string.Format(" And wu.Ceng ={0}", query.Ceng.Value);
            }
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("SELECT COUNT(1) FROM {0} {1};", tableName, where);
            //sb.AppendFormat("select {0} from {1} {2} {3} offset {4} rows fetch next {5} rows only", files, tableName, where, orderby, pageIndex, query.PageSize);
            sb.AppendFormat("SELECT {0} FROM {1} {2} {3} LIMIT {4},{5};", files, tableName, where, orderby, query.Start, query.Rows);
            using (var multi = _db.DbConnection.QueryMultiple(sb.ToString()))
            {
                var count = multi.ReadFirst<int>();

                var list = multi.Read<InventoryQueryResponse>();

                var rows = new List<InventoryQueryResponse>();

                var qty = list.Where(c => c.IsContainer == (int)IsContainer.实).Sum(c => c.Qty);

                foreach (var item in list)
                {
                    var row = rows.FirstOrDefault(c => c.ResourceId == item.ResourceId);
                    if (row != null)
                    {
                        //空
                        item.ResourceId = String.Empty;
                        rows.Add(item);
                    }
                    else
                        rows.Add(item);
                }

                var footers = new List<InventoryQueryResponse>()
                {
                    new InventoryQueryResponse(){
                        Id = -1,
                        Qty = qty,
                    }
                };

                return new DataGrid<InventoryQueryResponse>()
                {
                    Rows = rows,
                    Total = count,
                    Footer = footers,
                };
            }
        }

        #region 库存
        /// <summary>
        /// 库存查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataGrid<InventoryQueryResponse> InventoryQuery(InventoryQuery query)
        {
            var files = "ti.Id,ti.OrderType,ti.ResourceId,ti.WarehouseUnitId,wu.Code as WarehouseUnitTostring,ti.ContainerCode,ti.IsContainer,ti.Coordinate,ti.PartNo,ti.ProductName,ti.Grade,ti.LotNo,ti.Status,ti.Qty,ti.Remark,u.Account as CreatedByTostring,ti.CreateTime";
            var tableName = "t_inventory ti LEFT JOIN t_warehouseunit wu ON ti.WarehouseUnitId=wu.Id LEFT JOIN user u ON ti.CreatedBy=u.Id";
            var orderby = "ORDER BY ti.Id DESC";
            var where = "where 1=1";

            #region 条件
            //订单类型
            if (query.OrderType.HasValue)
                where += string.Format(" And ti.OrderType ={0}", query.OrderType.Value);
            //订单类型
            if (query.Type.HasValue)
                where += string.Format(" And wu.Type ={0}", query.Type.Value);
            //状态
            if (query.Status.HasValue)
                where += string.Format(" And ti.Status ={0}", query.Status.Value);
            //库存
            if (query.InventoryQty.HasValue)
                where += string.Format(" And ti.InventoryQty >{0}", query.InventoryQty.Value);
            //物料名称
            if (!string.IsNullOrEmpty(query.PartNo))
                where += string.Format(" And ti.PartNo like '%{0}%'", query.PartNo);
            //批号
            if (!string.IsNullOrEmpty(query.LotNo))
                where += string.Format(" And ti.LotNo like '%{0}%'", query.LotNo);
            //排
            if (query.Pai.HasValue)
                where += string.Format(" And wu.Pai ={0}", query.Pai.Value);
            //列
            if (query.Lie.HasValue)
                where += string.Format(" And wu.Lie ={0}", query.Lie.Value);
            //层
            if (query.Ceng.HasValue)
                where += string.Format(" And wu.Ceng ={0}", query.Ceng.Value);
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("SELECT COUNT(1) FROM {0} {1};", tableName, where);
            //sb.AppendFormat("select {0} from {1} {2} {3} offset {4} rows fetch next {5} rows only", files, tableName, where, orderby, pageIndex, query.PageSize);
            sb.AppendFormat("SELECT {0} FROM {1} {2} {3} LIMIT {4},{5};", files, tableName, where, orderby, query.Start, query.Rows);
            using (var multi = _db.DbConnection.QueryMultiple(sb.ToString()))
            {
                var count = multi.ReadFirst<int>();

                var list = multi.Read<InventoryQueryResponse>();

                var rows = new List<InventoryQueryResponse>();

                var qty = list.Where(c => c.IsContainer == (int)IsContainer.实).Sum(c => c.Qty);

                foreach (var item in list)
                {
                    var row = rows.FirstOrDefault(c => c.ResourceId == item.ResourceId);
                    if (row != null)
                    {
                        //空
                        item.ResourceId = String.Empty;
                        rows.Add(item);
                    }
                    else
                        rows.Add(item);
                }

                var footers = new List<InventoryQueryResponse>()
                {
                    new InventoryQueryResponse(){
                        Id = -1,
                        Qty = qty,
                    }
                };

                return new DataGrid<InventoryQueryResponse>()
                {
                    Rows = rows,
                    Total = count
                };
            }
        }

        /// <summary>
        /// 导出库存查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<InventoryQueryResponse> ExportInventoryQuery(InventoryQuery query)
        {
            var files = "ti.Id,ti.OrderType,ti.ResourceId,ti.WarehouseUnitId,wu.Code as WarehouseUnitTostring,ti.ContainerCode,ti.IsContainer,ti.Coordinate,ti.PartNo,ti.ProductName,ti.Grade,ti.LotNo,ti.Status,ti.Qty,ti.Remark,u.Account as CreatedByTostring,ti.CreateTime";
            var tableName = "t_inventory ti LEFT JOIN t_warehouseunit wu ON ti.WarehouseUnitId=wu.Id LEFT JOIN user u ON ti.CreatedBy=u.Id";
            var orderby = "ORDER BY ti.Id DESC";
            var where = "where 1=1";

            #region 条件
            if (query.OrderType.HasValue)
            {
                where += string.Format(" And ti.OrderType ={0}", query.OrderType.Value);
            }
            //状态
            if (query.Status.HasValue)
            {
                where += string.Format(" And ti.Status ={0}", query.Status.Value);
            }
            //容器类型
            if (query.ContainerType.HasValue)
            {
                where += string.Format(" And ti.ContainerType ={0}", query.ContainerType.Value);
            }
            //是否空托盘
            if (query.IsContainer.HasValue)
            {
                where += string.Format(" And ti.IsContainer ={0}", query.IsContainer.Value);
            }
            //库存
            if (query.InventoryQty.HasValue)
            {
                where += string.Format(" And ti.InventoryQty >{0}", query.InventoryQty.Value);
            }
            //订单号
            if (!string.IsNullOrEmpty(query.OrderId))
            {
                where += string.Format(" And ti.OrderId like '%{0}%'", query.OrderId);
            }
            //料号
            if (!string.IsNullOrEmpty(query.PartNo))
            {
                where += string.Format(" And ti.PartNo like '%{0}%'", query.PartNo);
            }
            //批号
            if (!string.IsNullOrEmpty(query.LotNo))
            {
                where += string.Format(" And ti.LotNo like '%{0}%'", query.LotNo);
            }
            //排
            if (query.Pai.HasValue)
            {
                where += string.Format(" And wu.Pai ={0}", query.Pai.Value);
            }
            //列
            if (query.Lie.HasValue)
            {
                where += string.Format(" And wu.Lie ={0}", query.Lie.Value);
            }
            //层
            if (query.Ceng.HasValue)
            {
                where += string.Format(" And wu.Ceng ={0}", query.Ceng.Value);
            }
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("SELECT {0} FROM {1} {2} {3};", files, tableName, where, orderby);

            var list = _db.DbConnection.Query<InventoryQueryResponse>(sb.ToString()).ToList();

            return list;
        }
        #endregion

        #region 报表
        /// <summary>
        /// 报表库存查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataGrid<InventoryQueryResponse> ReportInventoryQuery(InventoryQuery query)
        {
            var pageIndex = query.PageIndex <= 0 ? 0 : (query.PageIndex - 1) * query.PageSize;

            var files = "ti.Id,ti.OrderType,ti.ResourceId,ti.SourceId,ti.WarehouseUnitId,wu.Code as WarehouseUnitTostring,ti.ContainerCode,ti.IsContainer,ti.Coordinate,ti.PartNo,ti.ProductName,ti.Grade,ti.LotNo,ti.Status,ti.Qty,ti.Remark,u.Account as CreatedByTostring,ti.CreateTime";
            var tableName = "t_inventory ti LEFT JOIN t_warehouseunit wu ON ti.WarehouseUnitId=wu.Id LEFT JOIN user u ON ti.CreatedBy=u.Id";
            var orderby = "ORDER BY ti.Id DESC";
            var where = "where 1=1";

            #region 条件
            if (query.OrderType.HasValue)
                where += string.Format(" And ti.OrderType ={0}", query.OrderType.Value);
            //状态
            if (query.Status.HasValue)
                where += string.Format(" And ti.Status ={0}", query.Status.Value);
            //容器类型
            if (query.ContainerType.HasValue)
                where += string.Format(" And ti.ContainerType ={0}", query.ContainerType.Value);
            //是否空托盘
            if (query.IsContainer.HasValue)
                where += string.Format(" And ti.IsContainer ={0}", query.IsContainer.Value);
            //库存
            if (query.InventoryQty.HasValue)
                where += string.Format(" And ti.InventoryQty ={0}", query.InventoryQty.Value);
            //订单号
            if (!string.IsNullOrEmpty(query.OrderId))
                where += string.Format(" And ti.OrderId like '%{0}%'", query.OrderId);
            //物料名称
            if (!string.IsNullOrEmpty(query.ProductName))
                where += string.Format(" And ti.ProductName like '%{0}%'", query.ProductName);
            //批号
            if (!string.IsNullOrEmpty(query.LotNo))
                where += string.Format(" And ti.LotNo like '%{0}%'", query.LotNo);
            //排
            if (query.Pai.HasValue)
                where += string.Format(" And wu.Pai ={0}", query.Pai.Value);
            //列
            if (query.Lie.HasValue)
                where += string.Format(" And wu.Lie ={0}", query.Lie.Value);
            //层
            if (query.Ceng.HasValue)
                where += string.Format(" And wu.Ceng ={0}", query.Ceng.Value);
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
                where += string.Format(" And ti.CreateTime BETWEEN '{0}' AND '{1}'", query.StartTime, query.EndTime);
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("SELECT COUNT(1) FROM {0} {1};", tableName, where);
            //sb.AppendFormat("select {0} from {1} {2} {3} offset {4} rows fetch next {5} rows only", files, tableName, where, orderby, pageIndex, query.PageSize);
            sb.AppendFormat("SELECT {0} FROM {1} {2} {3} LIMIT {4},{5};", files, tableName, where, orderby, query.Start, query.Rows);
            using (var multi = _db.DbConnection.QueryMultiple(sb.ToString()))
            {
                var count = multi.ReadFirst<int>();

                var list = multi.Read<InventoryQueryResponse>();

                var rows = new List<InventoryQueryResponse>();

                var qty = list.Where(c => c.IsContainer == (int)IsContainer.实).Sum(c => c.Qty);

                foreach (var item in list)
                {
                    var row = rows.FirstOrDefault(c => c.ResourceId == item.ResourceId);
                    if (row != null)
                    {
                        //空
                        item.ResourceId = String.Empty;
                        rows.Add(item);
                    }
                    else
                        rows.Add(item);
                }

                var footers = new List<InventoryQueryResponse>()
                {
                    new InventoryQueryResponse(){
                        Id = -1,
                        Qty = qty,
                    }
                };

                return new DataGrid<InventoryQueryResponse>()
                {
                    Rows = list,
                    Total = count,
                    Footer = footers,
                };
            }
        }

        /// <summary>
        /// 导出库存查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<InventoryQueryResponse> ExportReportQuery(InventoryQuery query)
        {
            var files = "ti.Id,ti.OrderType,ti.ResourceId,ti.WarehouseUnitId,wu.Code as WarehouseUnitTostring,ti.ContainerCode,ti.IsContainer,ti.Coordinate,ti.PartNo,ti.ProductName,ti.Grade,ti.LotNo,ti.Status,ti.Qty,ti.Remark,u.Account as CreatedByTostring,ti.CreateTime";
            var tableName = "t_inventory ti LEFT JOIN t_warehouseunit wu ON ti.WarehouseUnitId=wu.Id LEFT JOIN user u ON ti.CreatedBy=u.Id";
            var orderby = "ORDER BY ti.Id DESC";
            var where = "where 1=1";

            #region 条件
            if (query.OrderType.HasValue)
                where += string.Format(" And ti.OrderType ={0}", query.OrderType.Value);
            //状态
            if (query.Status.HasValue)
                where += string.Format(" And ti.Status ={0}", query.Status.Value);
            //容器类型
            if (query.ContainerType.HasValue)
                where += string.Format(" And ti.ContainerType ={0}", query.ContainerType.Value);
            //是否空托盘
            if (query.IsContainer.HasValue)
                where += string.Format(" And ti.IsContainer ={0}", query.IsContainer.Value);
            //库存
            if (query.InventoryQty.HasValue)
                where += string.Format(" And ti.InventoryQty ={0}", query.InventoryQty.Value);
            //订单号
            if (!string.IsNullOrEmpty(query.OrderId))
                where += string.Format(" And ti.OrderId like '%{0}%'", query.OrderId);
            //料号
            if (!string.IsNullOrEmpty(query.PartNo))
                where += string.Format(" And ti.PartNo like '%{0}%'", query.PartNo);
            //批号
            if (!string.IsNullOrEmpty(query.LotNo))
                where += string.Format(" And ti.LotNo like '%{0}%'", query.LotNo);
            //排
            if (query.Pai.HasValue)
                where += string.Format(" And wu.Pai ={0}", query.Pai.Value);
            //列
            if (query.Lie.HasValue)
                where += string.Format(" And wu.Lie ={0}", query.Lie.Value);
            //层
            if (query.Ceng.HasValue)
                where += string.Format(" And wu.Ceng ={0}", query.Ceng.Value);
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
                where += string.Format(" And ti.CreateTime BETWEEN '{0}' AND '{1}'", query.StartTime, query.EndTime);
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("SELECT {0} FROM {1} {2} {3};", files, tableName, where, orderby);

            var list = _db.DbConnection.Query<InventoryQueryResponse>(sb.ToString()).ToList();

            return list;
        }
        #endregion

        #region 先进先出
        /// <summary>
        /// 先进先出
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JResult FIFO(FIFORequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                if (string.IsNullOrEmpty(request.PartNo))
                    throw new Exception("料号不能为空.");

                var inventorySql = @"select 
i.Id,
i.OrderType,
i.ResourceId,
i.SourceId,
w.`Code` as WarehouseUnitCode,
i.PartNo,
i.CreateTime 
from 
t_inventory i left join t_warehouseunit w on i.WarehouseUnitId=w.Id 
WHERE 
i.PartNo =@PartNo and i.OrderType=10 AND i.InventoryQty>0 AND i.Status in(0,1,2)
ORDER BY 
i.CreateTime ASC";

                var inventorys = _db.DbConnection.Query<InventoryResponse>(inventorySql, new { PartNo = request.PartNo });
                //获取第一条记录
                var inventory = inventorys.FirstOrDefault();
                if (inventory == null)
                    throw new Exception("查无记录.");
                if (inventory.PartNo != request.PartNo)
                {
                    response.Success = true;
                    response.Message = $"还有更早的物料({inventory.WarehouseUnitCode}),确定要继续出库?";
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return response;
        }
        #endregion
    }

}
